<!DOCTYPE HTML>
<html>
<head>
    <title>菜单管理</title>
    <script type="text/javascript" src="../js/common.js"></script>
    <link href="../js/bootstrap-treeview/bootstrap-treeview.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../js/bootstrap-treeview/bootstrap-treeview.js"></script>
</head>
<body>
<div class="pd-20">
    <div class="panel-body" style="padding-bottom:0px;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-4">菜单名称:</label>
                    <div class="col-sm-4">
                        <input id="search" type="text" class="form-control">
                    </div>
                    <div class="col-sm-4" style="text-align:left;">
                        <button type="button" class="btn btn-info" onclick="search()"><i
                                class="glyphicon glyphicon-search"></i>搜索
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="toolbar" class="btn-group">
        <!--<button id="btn_add" type="button" class="btn btn-default"
                onclick="add('添加权限','permission_add.html',500,450)">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default"
                onclick="edit('编辑权限','permission_add.html',500,450)">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="del()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
        </button>-->
        <span class="r">共有数据：<strong id="size">54</strong> 条</span>
    </div>
    <div id="tree"></div>
</div>
<script type="text/javascript">
    var menu_id = getQueryVariable("menu_id");
    loadButton(menu_id, "toolbar");
    initTree();
    //初始化菜单树
    function initTree() {
        $('#tree').treeview({
            data: getTree(),
            levels: 0,
            showTags: true,
            multiSelect: true
        });
    }

    //获取原数据
    function getTree() {
        var menu_data;
        requestService("/permission/", "get", false, "", function (result) {
            $("#size").text(result.data.length);
            menu_data = dataFormatTree(result.data, null);
        }, null);
        return menu_data;
    }

    //格式化数据(json树结构)
    function dataFormatTree(permissions, p_id) {
        var menu = [];
        for (var i = 0; i < permissions.length; i++) {
            var permission = permissions[i];
            if (permission.p_id == p_id) {
                var newNode = {
                    id: permission.id,
                    text: permission.permission_name,
                    state: {expanded: true},
                    nodes: dataFormatTree(permissions, permission.id)
                };
                menu.push(newNode);
            }
        }
        return menu;
    }

    function search() {
        var text = $("#search").val();
        $('#tree').treeview('search', [text, {
            ignoreCase: true,     // case insensitive
            exactMatch: false,    // like or equals
            revealResults: true  // reveal matching nodes
        }]);
    }

    /*添加*/
    function add() {
        var nodes = $('#tree').treeview("getSelected");
        if (nodes.length > 1) {
            layer.msg("最多只能选择一个节点");
            return;
        }
        var url = "permission_add.html?flag=1";
        if (nodes.length > 0) {
            url += "&p_id=" + nodes[0].id + "&p_text=" + nodes[0].text;
        }
        layer_show("添加权限", url, 500, 450);
    }

    /*编辑*/
    function edit() {
        var nodes = $('#tree').treeview("getSelected");
        if (nodes.length == 0) {
            layer.msg("必须要选择一个节点");
            return;
        }
        if (nodes.length > 1) {
            layer.msg("最多只能选择一个节点");
            return;
        }
        var node = nodes[0];
        var url = "permission_add.html?flag=0&id=" + node.id;
        var p_node = $('#tree').treeview('getParent', node);
        if (typeof(p_node.id) != 'undefined') {
            url += "&p_id=" + p_node.id + "&p_text=" + p_node.text;
        }
        layer_show("编辑权限", url, 500, 450);
    }

    /*删除*/
    function del() {
        var nodes = $('#tree').treeview("getSelected");
        if (nodes.length == 0) {
            layer.msg("最多只能选择一个节点");
            return;
        }

        var ids = [];
        $.each(nodes, function (i, r) {
            ids.push(r.id);
        });
        var data = {ids: ids};
        layer.confirm('确认要删除吗？节点下面的子节点也会被删除！', function () {
            requestService("/permission/del", "post", true, data, function (result) {
                layer.msg(result.desc);
                setTimeout(function () {
                    initTree();
                }, 500);
            }, null);
        });
    }
</script>
</body>
</html>